package com.fdb.efp.nls.api.rest.queue;

import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.util.TraceUtil;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.basic.sequence.service.facade.PatternedLimitableSeqService;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.efp.nls.service.facade.queue.NlsProcessBizHService;
import com.fdb.efp.nls.service.facade.queue.NlsProcessBizService;
import com.fdb.efp.nls.service.facade.sx.NlsCreditInfoService;
import com.fdb.efp.nls.service.vo.queue.NlsProcessBizHVO;
import com.fdb.efp.nls.service.vo.queue.NlsProcessBizVO;
import com.fdb.efp.nls.service.vo.sx.NlsCreditInfoVO;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Controller：网贷业务过程
 * 
 * @author huangwb
 * @since 2018年12月27日 上午8:58:24
 * @version 1.0
 */
@RestController
@RequestMapping("/api")
public class NlsProcessBizController extends AbstractController {

	@Autowired
	@Qualifier("nlsProcessBizService")
	private NlsProcessBizService nlsProcessBizService;

	@Autowired
	@Qualifier("nlsProcessBizHService")
	private NlsProcessBizHService nlsProcessBizHService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	@Autowired
	private PatternedLimitableSeqService patternedLimitableSeqService;

	@Autowired
	@Qualifier("nlsCreditInfoService")
	private NlsCreditInfoService nlsCreditInfoService;
	/**
	 * 列表数据查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@PostMapping(value = "/nls/process/bizs")
	public @ResponseBody ResponseData<List<NlsProcessBizVO>> queryNlsProcessBizAll(@RequestBody NlsProcessBizVO vo) {
		List<NlsProcessBizVO> outputVo = null;
		// 根据用户角色获取用户权限方案
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		//当网贷业务过程查询未配置数据权限时，默认查询本人所属机构及下属机构
		dataRuleType = dataRuleTypeMap == null ? DataRuleType.CurrDownOrg : dataRuleType;
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
			case Owner:
				outputVo = nlsProcessBizService.queryAllOwner(vo);
				break;
			case CurrOrg:
				outputVo = nlsProcessBizService.queryAllCurrOrg(vo);
				break;
			case CurrDownOrg:
				outputVo = nlsProcessBizService.queryAllCurrDownOrg(vo);
				break;
			case CurrOwnerPrd:
				outputVo = nlsProcessBizService.queryAllCurrOwnerPrd(vo);
				break;
			default:
				outputVo = nlsProcessBizService.queryAllCurrDownOrg(vo);
				break;
		}
		dealOutput(outputVo);
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@GetMapping(value = "/nls/process/biz/{applySeq}")
	public @ResponseBody ResponseData<Object> queryByPk(@PathVariable("applySeq") String applySeq) {
		String traceId = patternedLimitableSeqService.getSequence();
		ThreadContext.put(TraceUtil.TRACEID, traceId);
		TraceUtil.setTraceId(traceId);
		Object outputVo = null;
		// 1.先根据申请流水号查询当前表数据
		NlsProcessBizVO inVo = new NlsProcessBizVO();
		inVo.setApplySeq(applySeq);
		outputVo = nlsProcessBizService.queryByPk(inVo);
		// 2.当当前表不存在数据，查询历史表
		if (Objects.isNull(outputVo)) {
			NlsProcessBizHVO inHvo = new NlsProcessBizHVO();
			inHvo.setApplySeq(applySeq);
			outputVo = nlsProcessBizHService.queryByPk(inHvo);
		}
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键删除信息
	 * 
	 * @param nlsProcessBiz
	 * @return
	 */
	@DeleteMapping(value = "/nls/process/biz")
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody NlsProcessBizVO inNlsProcessBizVo) {
		String traceId = patternedLimitableSeqService.getSequence();
		ThreadContext.put(TraceUtil.TRACEID, traceId);
		TraceUtil.setTraceId(traceId);
		int outputVo = nlsProcessBizService.deleteByPk(inNlsProcessBizVo);
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inNlsProcessBizVo
	 * @return
	 */
	@PutMapping(value = "/nls/process/biz")
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody NlsProcessBizVO inNlsProcessBizVo) {
		String traceId = patternedLimitableSeqService.getSequence();
		ThreadContext.put(TraceUtil.TRACEID, traceId);
		TraceUtil.setTraceId(traceId);
		int outputVo = nlsProcessBizService.updateByPk(inNlsProcessBizVo);
		return getResponseData(outputVo);
	}

	/**
	 * 新增数据
	 * 
	 * @param inNlsProcessBizVo
	 * @return
	 */
	@PostMapping(value = "/nls/process/biz")
	public @ResponseBody ResponseData<Integer> insertNlsProcessBiz(@RequestBody NlsProcessBizVO inNlsProcessBizVo) {
		String traceId = patternedLimitableSeqService.getSequence();
		ThreadContext.put(TraceUtil.TRACEID, traceId);
		TraceUtil.setTraceId(traceId);
		int outputVo = nlsProcessBizService.insertNlsProcessBiz(inNlsProcessBizVo);
		return getResponseData(outputVo);
	}

	/**
	 * 百度产品页面展示 cusId取值授信申请信息表的cusId
	 * @author XIEZIWEI
	 * @params  * @param null
	 * @return
	 * @since 2020/4/28 15:55
	 * @date 2020/4/28 15:55
	 * @version 0.1
	 */

	private List<NlsProcessBizVO> dealOutput(List<NlsProcessBizVO> output){
		output.forEach( nlsProcessBizVO -> {
			if ("01010001".equals(nlsProcessBizVO.getPrdId()) || "01010002".equals(nlsProcessBizVO.getPrdId())){
				NlsCreditInfoVO nlsCreditInfoVO = new NlsCreditInfoVO();
				nlsCreditInfoVO.setLmtApplySeq(nlsProcessBizVO.getApplySeq());
				NlsCreditInfoVO queryNlsCreditInfoVO = nlsCreditInfoService.queryByPk(nlsCreditInfoVO);
				if (!Objects.isNull(queryNlsCreditInfoVO)){
					nlsProcessBizVO.setCusId(queryNlsCreditInfoVO.getCusId());
				}
			}
		});
		return output;
	}
}
